﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.Samples.Location;
using Mirkuz.Mapping;

namespace Mirkuz.TrailCompanion
{
    public class TrailManager
    {
        double _totaldistance = 0;        

        TimeSpan _elapsedtime = TimeSpan.Zero;        
        DateTime _starttime = DateTime.Now;
        
        string _startdatestring = DateTime.Now.ToString("yyyy-MM-dd-hh-mm");

        MapPoint _previousposition = null;        
        MapPoint _currentposition = null;

        public MapTrack Track;
        public WayPoint TargetWaypoint;

        public float AltitudeToTargetWaypoint;
        public double DistanceToTargetWaypoint;

        Boolean _running=false;
        
        public TrailManager()
        {            
        }

        public void Start(MapPoint StartPosition)
        {
            _startdatestring = DateTime.Now.ToString("yyyy-MM-dd-hh-mm");

            if (StartPosition == null)
                throw new NullReferenceException("Invalid Start Position");

            Track = new MapTrack();
            Track.AddStartPoint(StartPosition);

            _running = true;
            _starttime = DateTime.Now;
            
            _previousposition = StartPosition;            
                        
            _elapsedtime = TimeSpan.Zero;            
            _totaldistance = 0;
                               
        }

        public void Stop()
        {
            _running = false;

            Track.AddFinishPoint(_currentposition);
        }

        public bool IsRunning
        {
            get { return _running; }
        }

        /// <summary>
        /// Update the data with the new position
        /// </summary>
        /// <param name="Position"></param>
        public void UpdatePosition(MapPoint Position)
        {
            _currentposition = Position;

            if (_currentposition != null)
            {
                if (TargetWaypoint != null)
                {
                    DistanceToTargetWaypoint = TargetWaypoint.SurfaceDistanceMeters(_currentposition);
                    AltitudeToTargetWaypoint = TargetWaypoint.Altitude - _currentposition.Altitude;
                }
                else
                {
                    DistanceToTargetWaypoint = 0;
                    AltitudeToTargetWaypoint = 0;
                }
            }

            if (!_running)
                return;

            if (_previousposition != null && _currentposition != null)
                _totaldistance += _currentposition.SurfaceDistanceMeters(_previousposition);                

            if (_currentposition != null)
            {
                Track.AddPoint(_currentposition);
                             
                _previousposition = _currentposition;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Tick()
        {
            if (!_running)
                return;

            //calcolo del tempo trascorso dallo start
            _elapsedtime = DateTime.Now.Subtract(_starttime);      
      
        }

        public double RunningDistance
        {
            get { return _totaldistance; }
        }        
        public TimeSpan ElapsedTime
        {
            get { return _elapsedtime; }
        }               

        public void SetTarget(WayPoint t)
        {
            TargetWaypoint=t;
        }

        public void SaveTrail()
        {            
            try
            {
                //salva file KML
                string kmlfilename = "TC-" + _startdatestring + ".kml";

                Mirkuz.Mapping.KMLCreator kmlcreator = new Mirkuz.Mapping.KMLCreator(kmlfilename);
                kmlcreator.CreateKML();
                kmlcreator.AddLine(Track);
                kmlcreator.CloseKML();               
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }
    }
}
